from django.db.models.fields.related import ForeignKey
# from cews.models import WorkOrderList
from django.db import models
import datetime

# import json
import string

# import django.db.models.fields.related.ForeignKey as ForeignKey

delete_dict = {sp_character: '' for sp_character in string.punctuation}
delete_dict[' '] = '_'
table = str.maketrans(delete_dict)

def trimVerboseNames():
    """
        Returns list of escaped verbose names with out FK fields
    """
    ret = []
    return []
    ## TODO REVAMP THIS
    # for f in WorkOrderList._meta.fields:
    #     if isinstance(f,models.ForeignKey):
    #         continue
    #     ret.append({'verbose':f.verbose_name,'field':f,'key':f.verbose_name.translate(table)})
        
    # return ret

def translateField(verbose_name):
    return verbose_name.translate(table)

def getFormData(wo):
    """
        Return key value pair from workorder to fill in new form
    """
    ret ={}
    fields = trimVerboseNames()
    for field in fields:
        val  =getattr(wo,field['field'].name)
        if isinstance(val ,datetime.datetime):
            val = val.strftime("%Y-%m-%d %H:%M:%S")
        
        if isinstance(val ,datetime.date):
            val = val.strftime("%Y-%m-%d")
        
        ret[field['field'].name] = val
    
    return ret
    


def getComponents():
    """
        Returns components object
    """
    key_verbose_pair = trimVerboseNames()
    components = {}
    components['serialNo'] = {
        'title': 'Serial Number',
        'key': 'serialNo',
        'icon': 'terminal',
        'schema': {
            'label': 'Serial Number',
            'type': 'textfield',
            'key': 'serialNo',
            'input': True,
        }
    }
    for pa in key_verbose_pair:
        widget = False
        icon="terminal"
        if isinstance(pa['field'],(models.IntegerField,models.FloatField,models.BigIntegerField)):
            field_type="number"
            icon='hashtag'
        elif isinstance(pa['field'],(models.DateTimeField)):
            field_type = "datetime"
            icon='calendar-alt'
            widget = {
                'type': 'calendar',
                'displayInTimezone': 'viewer',
                'locale': 'en',
                'useLocaleSettings': True,
                'allowInput': True,
                'mode': 'single',
                'enableTime': True,
                'noCalendar': True,
                'format': 'yyyy-MM-dd hh:mm',
                'hourIncrement': 1,
                'minuteIncrement': 1,
                'time_24hr': True,
                'minDate': "",
                'disableWeekends': True,
                'disableWeekdays': True,
                'maxDate': ""
            }
        elif isinstance(pa['field'],(models.DateField)):
            field_type = "datetime"
            icon='calendar-alt'
            widget = {
                'type': 'calendar',
                'displayInTimezone': 'viewer',
                'locale': 'en',
                'useLocaleSettings': True,
                'allowInput': True,
                'mode': 'single',
                'enableTime': True,
                'noCalendar': True,
                'format': 'yyyy-MM-dd',
                'hourIncrement': 1,
                'minuteIncrement': 1,
                'time_24hr': True,
                'minDate': "",
                'disableWeekends': True,
                'disableWeekdays': True,
                'maxDate': ""
            }
        else:
            field_type = "textfield"

        components[pa['key']] ={
            'title': pa['verbose'],
                'key': pa['key'],
                'icon': icon,
                'schema': {
                    'label': pa['verbose'],
                    'type': field_type,
                    'key': pa['key'],
                    'input': True,
                },
                'widget':widget
        }
    return components